package com.tanhua.manager.listener;

import com.tanhua.commons.template.ContentCensorTemplate;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.PublishApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 审核动态内容
 *
 * @author anshuai
 * @create 2021-02-24 17:22
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = "tanhua-publish", consumerGroup = "tanhua-publish-group")
public class PublishCensorMessageListener implements RocketMQListener<String> {

    @Reference
    private PublishApi publishApi;

    @Autowired
    private ContentCensorTemplate contentCensorTemplate;

    @Override
    public void onMessage(String message) {
        log.info("====开始审核动态内容，动态id:{}", message);

        //1.查询动态
        Publish publish = publishApi.findById(message);

        //2.审核文本内容
        boolean textOk = contentCensorTemplate.textCensor(publish.getTextContent());

        //3. 审核图片内容
        boolean imageOk = contentCensorTemplate.imageBatchCensor(publish.getMedias());

        //4. 更新审核结果
        //1:已审核   2:已驳回
        publish.setState(textOk && imageOk ? 1 : 2);
        publishApi.updateState(publish);

        log.info("====审核动态完成，动态id：{}，结果是：{}", message, textOk && imageOk);
    }
}
